home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / caldr / caldr1.frm (.txt) next >
Encoding:
Visual Basic Form  |  1995-05-08  |  13.3 KB  |  452 lines

  1. VERSION 2.00
  2. Begin Form Form1 
  3.    BackColor       =   &H00C0C0C0&
  4.    Caption         =   "Calendar Demo"
  5.    ClientHeight    =   4020
  6.    ClientLeft      =   1080
  7.    ClientTop       =   1485
  8.    ClientWidth     =   7365
  9.    Height          =   4425
  10.    Left            =   1020
  11.    LinkTopic       =   "Form1"
  12.    ScaleHeight     =   268
  13.    ScaleMode       =   3  'Pixel
  14.    ScaleWidth      =   491
  15.    Top             =   1140
  16.    Width           =   7485
  17.    Begin CommandButton Command1 
  18.       Caption         =   "Calendar"
  19.       Height          =   525
  20.       Left            =   1140
  21.       TabIndex        =   7
  22.       Top             =   165
  23.       Width           =   1665
  24.    End
  25.    Begin PictureBox Calendar 
  26.       AutoRedraw      =   -1  'True
  27.       BackColor       =   &H00C0C0C0&
  28.       ForeColor       =   &H00800000&
  29.       Height          =   1800
  30.       Left            =   4590
  31.       ScaleHeight     =   118
  32.       ScaleMode       =   3  'Pixel
  33.       ScaleWidth      =   147
  34.       TabIndex        =   0
  35.       Top             =   255
  36.       Visible         =   0   'False
  37.       Width           =   2235
  38.       Begin SSCommand btnMonth 
  39.          BevelWidth      =   0
  40.          Caption         =   "Command3D1"
  41.          Font3D          =   0  'None
  42.          Height          =   285
  43.          Index           =   0
  44.          Left            =   615
  45.          TabIndex        =   5
  46.          Top             =   570
  47.          Width           =   315
  48.       End
  49.       Begin SSCommand btnYear 
  50.          BevelWidth      =   0
  51.          Caption         =   "Command3D1"
  52.          Font3D          =   0  'None
  53.          Height          =   300
  54.          Index           =   0
  55.          Left            =   1005
  56.          TabIndex        =   2
  57.          Top             =   405
  58.          Width           =   315
  59.       End
  60.       Begin PictureBox Picture1 
  61.          AutoRedraw      =   -1  'True
  62.          BackColor       =   &H00C0C0C0&
  63.          Height          =   375
  64.          Left            =   120
  65.          ScaleHeight     =   23
  66.          ScaleMode       =   3  'Pixel
  67.          ScaleWidth      =   24
  68.          TabIndex        =   1
  69.          Top             =   645
  70.          Width           =   390
  71.          Begin PictureBox Picture2 
  72.             BackColor       =   &H00000000&
  73.             BorderStyle     =   0  'None
  74.             ForeColor       =   &H00C0C0C0&
  75.             Height          =   165
  76.             Left            =   90
  77.             ScaleHeight     =   11
  78.             ScaleMode       =   3  'Pixel
  79.             ScaleWidth      =   14
  80.             TabIndex        =   6
  81.             Top             =   75
  82.             Width           =   210
  83.          End
  84.       End
  85.       Begin Label lblMonth 
  86.          BorderStyle     =   1  'Fixed Single
  87.          Caption         =   "Label1"
  88.          Height          =   165
  89.          Left            =   180
  90.          TabIndex        =   4
  91.          Top             =   330
  92.          Width           =   630
  93.       End
  94.       Begin Label lblYear 
  95.          BorderStyle     =   1  'Fixed Single
  96.          Caption         =   "Label1"
  97.          Height          =   210
  98.          Left            =   225
  99.          TabIndex        =   3
  100.          Top             =   120
  101.          Width           =   870
  102.       End
  103.    End
  104.    Begin Label Label1 
  105.       BackColor       =   &H00C0C0C0&
  106.       Caption         =   "Click button to display calendar.  Clicking on a date highlights and sets it. A set date can be removed by double clicking on it.  Feel free to improve upon.  Any suggestions to speed display when you advance or retard the calendar with the arrows would be appreciated. This just shows what's possible with VB, alone.  Much was suggested by  VB Knowledge Base article,  ""How to Make a Spreadsheet-style Grid that Allows Editing"". I do contract programming in VB and Access and would appreciate any leads for work you can pass my way.                                        Glenn Silverman :  CompuServe 71450,2750"
  107.       Height          =   2820
  108.       Left            =   225
  109.       TabIndex        =   8
  110.       Top             =   825
  111.       Width           =   3945
  112.    End
  113. 'Max grid size
  114. Const grid_col_max = 10
  115. Const grid_row_max = 20
  116. 'grid dimensions
  117. Dim w As Single
  118. Dim h As Single
  119. 'Current grid size
  120. Dim grid_cols As Integer
  121. Dim grid_rows As Integer
  122. 'Current cell position
  123. Dim grid_col As Integer
  124. Dim grid_row As Integer
  125. 'Grid string contents
  126. Dim grid_text(35) As String
  127. 'Grid cell numbers
  128. Dim cell(35) As Rect
  129. 'Grid line positions
  130. Dim grid_line_col(grid_col_max) As Integer
  131. Dim grid_line_row(grid_row_max) As Integer
  132. 'Calendar date setting
  133. Dim calDate As Long
  134. Sub AdvanceMonth ()
  135.     Dim c, m, y, ds
  136.     c = DateValue(lblMonth + " 1, " + lblYear)
  137.     m = Month(c) + 1
  138.     If m = 13 Then
  139.         m = 1
  140.         y = Year(c) + 1
  141.     Else
  142.         y = Year(c)
  143.     End If
  144.     ds = DateSerial(y, m, 1)
  145.     ShowMonth ds
  146. End Sub
  147. Sub AdvanceYear ()
  148.     Dim c, m, y, ds
  149.     c = DateValue(lblMonth + " 1, " + lblYear)
  150.     y = Year(c) + 1
  151.     m = Month(c)
  152.     ds = DateSerial(y, m, 1)
  153.     ShowMonth ds
  154. End Sub
  155. Sub btnMonth_Click (Index As Integer)
  156.     If Index = 0 Then
  157.         RetardMonth
  158.     Else
  159.         AdvanceMonth
  160.     End If
  161. End Sub
  162. Sub btnYear_Click (Index As Integer)
  163.     If Index = 1 Then
  164.         AdvanceYear
  165.     Else
  166.         RetardYear
  167.     End If
  168. End Sub
  169. Sub BuildCal ()
  170.     Dim i, l, t
  171.     ReDim DaysOfWeek(7) As String
  172.     Dim x1 As Integer
  173.     Dim x2 As Integer
  174.     Dim y1 As Integer
  175.     Dim y2 As Integer
  176.     DaysOfWeek(0) = "S"
  177.     DaysOfWeek(1) = "M"
  178.     DaysOfWeek(2) = "T"
  179.     DaysOfWeek(3) = "W"
  180.     DaysOfWeek(4) = "T"
  181.     DaysOfWeek(5) = "F"
  182.     DaysOfWeek(6) = "S"
  183.     'Set control dimensions
  184.     h = Calendar.Height / 8
  185.     w = Calendar.Width / 7
  186.     'Set headings
  187.     SetControl lblYear, h, 5 * w + 1, " ", &HC0C0C0
  188.     ControlText lblYear, True, &HC0, 2
  189.     lblYear.Move w, 0
  190.     SetControl lblMonth, h, 5 * w + 1, " ", &HC0C0C0
  191.     ControlText lblMonth, True, &HC0, 2
  192.     lblMonth.Move w, h
  193.    'set weekday heads
  194.    For i = 0 To 6
  195.     x1 = i * w + 10
  196.     y1 = 2 * h + 3
  197.     x2 = (i + 1) * w - 1
  198.     y2 = 3 * h - 1
  199.     Calendar.CurrentX = x1 - 6 + (x2 - x1 - Picture1.TextWidth(DaysOfWeek(i))) / 2
  200.     Calendar.CurrentY = y1 + (y2 - y1 - Picture1.TextHeight(DaysOfWeek(i))) / 2
  201.     Calendar.Print DaysOfWeek(i)
  202.    Next i
  203.    'set grdCal
  204.     Picture1.Move 0, 3 * h, 7 * w, 5 * h
  205.     Picture2.Visible = False
  206.    'build cal grid
  207.     grid_build 7, 5
  208.    'Set buttons
  209.     btnYear(0).Move 0, 0, w, h
  210.     btnYear(0).Caption = "<"
  211.     Load btnYear(1)
  212.     btnYear(1).Visible = True
  213.     btnYear(1).Move 6 * w, 0
  214.     btnYear(1).Caption = ">"
  215.     btnMonth(0).Move 0, h, w, h
  216.     btnMonth(0).Caption = "<"
  217.     Load btnMonth(1)
  218.     btnMonth(1).Visible = True
  219.     btnMonth(1).Move 6 * w, h
  220.     btnMonth(1).Caption = ">"
  221. End Sub
  222. Sub Command1_Click ()
  223.     Calendar.Visible = True
  224. End Sub
  225. Sub ControlText (c As Control, wt, tcol, al)
  226.     c.FontBold = wt
  227.     c.ForeColor = tcol
  228.     c.Alignment = al
  229. End Sub
  230. Function date_set (col As Integer, row As Integer) As Long
  231.     date_set = DateValue(lblMonth + " " + grid_text(row * 7 + col) + ", " + lblYear)
  232. End Function
  233. Sub DayCalc (first, days)
  234.     Dim d, nday
  235.     Dim i As Integer
  236.     Dim c As Rect
  237.     Dim x1 As Integer
  238.     Dim x2 As Integer
  239.     Dim y1 As Integer
  240.     Dim y2 As Integer
  241.     Dim txtColor As Long
  242.     Dim col As Integer
  243.     Dim row As Integer
  244.     For i = 0 To 33 + first
  245.        c = cell(i Mod 35)
  246.        x1 = c.upper.x
  247.        y1 = c.upper.y
  248.        x2 = c.lower.x
  249.        y2 = c.lower.y
  250.        
  251.        'clear cell
  252.        Picture1.Line (x1 + 1, y1 + 1)-(x2 - 1, y2 - 1), Picture1.BackColor, BF
  253.         
  254.        d = i - first + 2
  255.        If d < 1 Or d > days Then
  256.           nday = " "
  257.        Else
  258.           nday = d
  259.        End If
  260.        'display day number in cell
  261.         Picture1.CurrentX = x1 - 6 + (x2 - x1 - Picture1.TextWidth(nday)) / 2
  262.         Picture1.CurrentY = y1 + (y2 - y1 - Picture1.TextHeight(nday)) / 2
  263.         
  264.         If nday = Day(Date) And lblMonth = Format(Date, "mmmm") And lblYear = Format(Date, "yyyy") Then
  265.             txtColor = Picture1.ForeColor
  266.             Picture1.ForeColor = RGB(255, 0, 0)
  267.             Picture1.Print nday
  268.             Picture1.ForeColor = txtColor
  269.         Else
  270.             Picture1.Print nday
  271.         End If
  272.        'store day in grid_text array
  273.         grid_text(i Mod 35) = nday
  274.    Next i
  275.    'calendar date setting
  276.    If lblMonth = Format(calDate, "mmmm") And lblYear = Format(calDate, "yyyy") Then
  277.         c = cell(Day(calDate) - first + 2)
  278.         set_date c.upper.x + 1, c.upper.y + 1
  279.    Else
  280.         Picture2.Visible = False
  281.    End If
  282. End Sub
  283. Sub draw_grid_lines ()
  284.     For i = 0 To grid_cols
  285.         x2% = grid_line_col(i)
  286.         y2% = grid_line_row(grid_rows)
  287.         Picture1.Line (grid_line_col(i), 0)-(x2%, y2%), &H808080
  288.     Next
  289.     For i = 0 To grid_rows
  290.         x2% = grid_line_col(grid_cols)
  291.         y2% = grid_line_row(i)
  292.         Picture1.Line (0, grid_line_row(i))-(x2%, y2%), &H808080
  293.     Next
  294. End Sub
  295. Sub fill_cell_array ()
  296.     Dim col As Integer
  297.     Dim row As Integer
  298.     Dim p1 As Pt
  299.     Dim p2 As Pt
  300.     Dim rt As Rect
  301.     For row = 0 To 4
  302.         For col = 0 To 6
  303.             p1.x = grid_line_col(col) + 1
  304.             p1.y = grid_line_row(row) + 1
  305.             p2.x = grid_line_col(col + 1) - 1
  306.             p2.y = grid_line_row(row + 1) - 1
  307.             rt.upper = p1
  308.             rt.lower = p2
  309.             cell(row * 7 + col) = rt
  310.         Next
  311.     Next
  312. End Sub
  313. Sub Form_Load ()
  314.     BuildCal
  315.     ShowMonth Date
  316. End Sub
  317. Sub grid_build (Cols As Integer, Rows As Integer)
  318.     'set grid size
  319.     grid_cols = Cols
  320.     grid_rows = Rows
  321.     'remove borders
  322.     Picture1.BorderStyle = 0
  323.     'set col widths and row heights
  324.     Dim i As Integer
  325.     Dim d As Integer
  326.     d = 0
  327.     For i = 0 To UBound(grid_line_col)
  328.         grid_line_col(i) = d
  329.         d = d + w
  330.     Next
  331.     d = 0
  332.     For i = 0 To UBound(grid_line_row)
  333.         grid_line_row(i) = d
  334.         d = d + h
  335.     Next
  336.     draw_grid_lines
  337.    'fill cell array
  338.     Call fill_cell_array
  339.     'create grid shadows
  340.     shadow_grid
  341. End Sub
  342. Sub grid_cell_move (col As Integer, row As Integer)
  343.     Dim x1 As Integer
  344.     Dim x2 As Integer
  345.     Dim y1 As Single
  346.     Dim y2 As Single
  347.     Dim nday
  348.     'set new grid current cell
  349.     grid_col = col
  350.     grid_row = row
  351.     'Move label box to new cell
  352.      x1 = grid_line_col(grid_col)
  353.      y1 = grid_line_row(grid_row)
  354.      x2 = grid_line_col(grid_col + 1) - x1
  355.      y2 = grid_line_row(grid_row + 1) - y1
  356.      Picture2.Move x1 + 1, y1 + 1, x2 - 2, y2 - 2
  357.      Picture2.Visible = True
  358.     'Copy contents of new cell to label
  359.      Picture2.Cls
  360.      nday = grid_text(row * 7 + col)
  361.      Picture2.CurrentX = -3 + (x2 - Picture1.TextWidth(nday)) / 2
  362.      Picture2.CurrentY = -.75 + (y2 - Picture1.TextHeight(nday)) / 2
  363.      Picture2.Print nday
  364. End Sub
  365. Sub Picture1_MouseDown (Button As Integer, Shift As Integer, x As Single, y As Single)
  366.     set_date x, y
  367. End Sub
  368. Sub Picture2_DblClick ()
  369.     Picture2.Visible = False
  370.     calDate = 0
  371. End Sub
  372. Sub RetardMonth ()
  373.     Dim c, m, y, ds
  374.     c = DateValue(lblMonth + " 1, " + lblYear)
  375.     m = Month(c) - 1
  376.     If m = 0 Then
  377.         m = 12
  378.         y = Year(c) - 1
  379.     Else
  380.         y = Year(c)
  381.     End If
  382.     ds = DateSerial(y, m, Day(Date))
  383.     ShowMonth ds
  384. End Sub
  385. Sub RetardYear ()
  386.     Dim c, m, y, ds
  387.     c = DateValue(lblMonth + " 1, " + lblYear)
  388.     y = Year(c) - 1
  389.     m = Month(c)
  390.     ds = DateSerial(y, m, 1)
  391.     ShowMonth ds
  392. End Sub
  393. Sub set_date (x As Single, y As Single)
  394.     Dim col As Integer
  395.     Dim row As Integer
  396.     'Find cell clicked in
  397.     col = grid_col
  398.     row = grid_row
  399.     For i = 0 To grid_cols - 1
  400.         If x >= grid_line_col(i) And x < grid_line_col(i + 1) Then
  401.             col = i
  402.             Exit For
  403.         End If
  404.     Next
  405.     For i = 0 To grid_rows - 1
  406.         If y >= grid_line_row(i) And y < grid_line_row(i + 1) Then
  407.             row = i
  408.             Exit For
  409.         End If
  410.     Next
  411.     If grid_text(row * 7 + col) <> " " Then
  412.         Call grid_cell_move(col, row)
  413.         
  414.         'set the new date
  415.         calDate = date_set(col, row)
  416.     End If
  417. End Sub
  418. Sub SetControl (c As Control, ht, wd, cpt, bkCol)
  419.     c.Height = ht
  420.     c.Width = wd
  421.     c.Caption = cpt
  422.     c.BackColor = bkCol
  423. End Sub
  424. Sub shadow_grid ()
  425.     Dim c As Rect
  426.     Dim i As Integer
  427.     Dim x1 As Integer
  428.     Dim x2 As Integer
  429.     Dim y1 As Integer
  430.     Dim y2 As Integer
  431.     For i = 0 To 34
  432.        c = cell(i)
  433.        x1 = c.upper.x
  434.        y1 = c.upper.y
  435.        x2 = c.lower.x
  436.        y2 = c.lower.y
  437.        
  438.        Picture1.Line (x1, y2)-(x2, y2), RGB(255, 255, 255)
  439.        Picture1.Line (x2, y1)-(x2, y2), RGB(255, 255, 255)
  440.     Next
  441. End Sub
  442. Sub ShowMonth (dt)
  443.     Dim date1, date2, days, first, currIndex
  444.     lblYear = Format(dt, "yyyy")
  445.     lblMonth = Format(dt, "mmmm")
  446.     date1 = DateSerial(Year(dt), Month(dt), 1)
  447.     date2 = DateAdd("m", 1, date1)
  448.     days = DateDiff("d", date1, date2)
  449.     first = Weekday(date1)
  450.     DayCalc first, days
  451. End Sub
  452.